package com.wu.Backstage.check;

import cn.hutool.core.date.DateUtil;
import com.wu.Backstage.config.EasyPoiConfig;
import com.wu.Backstage.entity.Po.CommodityEntityPo;
import com.wu.Backstage.entity.Po.UserSearch;
import com.wu.Backstage.entity.Po.adminPo;
import com.wu.Backstage.entity.UserAccount;
import com.wu.Backstage.service.UserService;
import com.wu.Backstage.service.adminService;
import com.wu.Backstage.tool.PageHelperUtil;
import com.wu.Backstage.tool.Tool;
import com.wu.Backstage.util.JsonUtil;
import com.wu.Backstage.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;



@Component
public class MiddlelayerCheck {

    @Autowired
    private UserService userService;

    public static MiddlelayerCheck oftenCheck;

    @Autowired
    private adminService userEntityService;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private com.wu.Backstage.service.comprehensiveService comprehensiveService;


    @PostConstruct
    public void init() {
        oftenCheck = this;
        oftenCheck.userService = this.userService;
        // 初使化时将已静态化的otherService实例化
    }


    /**搜索商品**/
    public Tool finSearchProducts (String name,String email)
    {
        ArrayList<HashMap<String,Object>> fincom =  oftenCheck.userService.finSearchProducts(name);
        java.util.Date date = DateUtil.date();
        UserSearch user = new UserSearch();
        user.setEmail(email);
        user.setName(name);
        user.setLogintime(date);
        comprehensiveService.InUserSearch(user);
        if(fincom.size()<=0)
        {
            return  isUserAccount(null,"查询失败,无当前商品!");
        }else{
            return  isUserAccount(fincom,"查询成功!");
        }
    }


    /**查询商品评价**/
    public Tool findevaluated(String dityid)
    {
        ArrayList<HashMap<String,Object>> findeva = oftenCheck.userService.findevaluated(dityid);
        if(findeva.size()<=0)
        {
            return  isUserAccount(null,"查询失败,或者当前商品无评价!");
        }else{
            return  isUserAccount(findeva,"查询成功!");
        }
    }

    public Tool  findComserCount()
    {
        ArrayList<HashMap<String,Object>> list =   userService.findCommCount();
        if(list.size()<=0)
        {
            return  isUserAccount(null,"查询失败!");
        }else{
            return  isUserAccount(list,"查询成功!");
        }
    }



    public Tool findComCount(Integer id)
    {
        int count = userService.ComCount(id);
        if(count<=0)
        {
            return  isUserAccount(null,"浏览记录记录失败!");
        }else{
            return  isUserAccount(1,"浏览记录成功!");
        }
    }


    /**根据email查询订单数据**/
    public Tool findOrder(String email,Integer page,Integer num){

        if(findUserByEmial(email).getSuccess()==false)
        {
            return  isUserAccount(null,"当前用户不存在!");
        }
        ArrayList<HashMap<String,Object>> splist =oftenCheck.userService.findOrder(email,page,num);
        if(splist!=null && splist.size()>0){
            return  isUserAccount(splist,"查询成功!");
        }
        return  isUserAccount(null,"无数据!");
    }





    /**根据email查询购物车**/
    public Tool findshoppingCard(String email)
    {
        if(findUserByEmial(email).getSuccess()==false)
        {
            return  isUserAccount(null,"当前用户不存在!");
        }
        ArrayList<HashMap<String,Object>> splist =oftenCheck.userService.findshoppingCard(email);
        if(splist!=null && splist.size()>0){
            return  isUserAccount(splist,"查询成功!");
        }
        return  isUserAccount(null,"无数据!");
    }



    public Tool findCount(String email){

        int count = oftenCheck.userService.findCount(email);
        if(count>0){
            return  isUserAccount(count,"查询成功!");
        }else{
            return isUserAccount(null,"当前数据为空");
        }

    }


    public  ArrayList<HashMap<String,Object>> findCommodityEntityPoSp(Integer sta, Integer st)
    {
        ArrayList<HashMap<String,Object>> Commodity = oftenCheck.userService.findCommodityEntityPoSp(sta,st);
        return Commodity;
    }

    /**
     * 查询所有信息
     **/
    public List<UserAccount> getAllUsers() {
        List<UserAccount> userAccount = oftenCheck.userService.getAllUser();
        return userAccount;
    }

    /**根据指定id删除数据**/
    public Tool DataleUsre(Integer id)
    {
        int isTrue = oftenCheck.userService.DataleUsre(id);
        if(isTrue>0){
            return isUserAccount(1,"数据删除成功!");
        }else{
            return isUserAccount(null,"数据删除失败!");
        }
    }

    /**
     * 根据指定id查询数据
     **/
    public Tool findUserId(Integer id) {
        UserAccount userAccount = oftenCheck.userService.getAllUserById(id);
        if(userAccount==null){
            return isUserAccount(null,"没有查询到相关数据!");
        }else{
            return isUserAccount(userAccount,"数据查询成功!");
        }
    }


    /**
     * 根据指定email
     **/
    public Tool findUserByEmial(String email) {
        UserAccount userAccount = oftenCheck.userService.getAllUserEmail(email);
        if(userAccount==null){
            return isUserAccount(null,"没有查询到相关数据!");
        }else{
            return isUserAccount(userAccount,"数据查询成功!");
        }
    }


    /**
     * 根据指定name查询
     **/
    public Tool findUserByName(String name) {
        UserAccount userAccount = oftenCheck.userService.getAllUserByName(name);
        if(userAccount==null){
            return isUserAccount(null,"没有查询到相关数据!");
        }else{
            return isUserAccount(userAccount,"数据查询成功!");
        }

    }

    /**注册**/
    public Tool logounPage(String email, String password, String name, Integer statuscode, String sex, Integer age, String dsc)
    {
        Utils unit = new Utils();
        Date date = unit.getDate();
        Time time = unit.getTime();
        int isTrue = oftenCheck.userService.AddUser(email, password, name,date,time,statuscode,sex,age,dsc);
        if(isTrue>0){
            return isUserAccount(isTrue,"注册成功");
        }else{
            return isUserAccount(isTrue,"注册失败");
        }

    }

    /**修改登录状态**/
    public boolean upadteStat(String email)
    {
        int isT = oftenCheck.userService.upadteStat(email);
        if(isT<=0) return false;
        return true;
    }


    /**修改数据**/
    public Tool UpdateUser(String email,String name,String password,String statuscode,Integer age,String sex,String dsc,Integer id)
    {
        Integer statuscodes = 1;
        if(statuscode.equals("异常"))
        {
            statuscodes = 0;
        }
        int isT = oftenCheck.userService.UpdateUsre(email,name,password,statuscodes,age,sex,dsc,id);
        if(isT>0){
            return  isUserAccount(1,"修改成功!");
        }else{
            return  isUserAccount(null,"修改失败!");
        }
    }

    /**修改数据**/
    public Tool UserUpdateUser(String name,String password,Integer age,String sex,String dsc,Integer id,String img)
    {
        int isT = oftenCheck.userService.UserUpdateUsre(name,password,age,sex,dsc,id,img);
        if(isT>0){
            return  isUserAccount(1,"修改成功!");
        }else{
            return  isUserAccount(null,"修改失败!");
        }
    }


    /**批量删除数据**/
    public Tool DataleUsres(String ids)
    {
        String[] str = ids.split(",");
        List list = Arrays.asList(str);
        AtomicInteger isT = new AtomicInteger();
        list.forEach(id->{
            Integer isId = Integer.parseInt((String) id);
            isT.set(oftenCheck.userService.DataleUsre(isId));
        });

        if(isT.get() <=0)  return isUserAccount(null,"删除失败!");
        return isUserAccount(1,"删除成功!");
    }


    /**
     * 分页查询
     * **/
    public Tool getPageHelper(int page,int pagesize)
    {
        PageHelperUtil<UserAccount> PageHelperUtil = oftenCheck.userService.getUserAccountPage(page,pagesize);
        if(PageHelperUtil==null){
            return isUserAccount(null,"没有查询到任何信息!");
        }else{
            return isUserAccount(PageHelperUtil,"信息查询成功!");
        }
    }

    /***
     * 导出excel
     * **/
    public Tool getUserExcel()
    {
        EasyPoiConfig easy = new EasyPoiConfig();
        try {
            easy.putexcel();
            return isUserAccount(1,"信息导出成功!");
        } catch (IOException e) {
            e.printStackTrace();
            return isUserAccount(null,"信息导出失败!");
        }
    }



    /**管理员登录**/
    public Tool adlogin(String email,String password)
    {

        adminPo User = userEntityService.findUserEntityPoEmail(email);
        if(User==null)
        {
            return oftenCheck.isUserAccount(null,"登录失败!当前邮箱未注册...");
        }

        adminPo Users = userEntityService.login(email,password);
        if(Users==null)
        {
            return oftenCheck.isUserAccount(null,"登录失败!账号或者密码错误...");
        }
        return oftenCheck.isUserAccount(Users,"恭喜您,登录成功!");
    }


    /**用户注册数据**/
    public Tool adaddUser(adminPo userEntity)
    {

        adminPo User = userEntityService.findUserEntityPoEmail(userEntity.getEmail());
        System.out.println("addUser:"+User);
        if(User!=null)
        {
            return oftenCheck.isUserAccount(null,"注册失败!当前邮箱已被注册...");
        }

        adminPo UserEntity = userEntityService.AddUserEntityPo(userEntity);
        if(UserEntity!=null)
        {
            return oftenCheck.isUserAccount("1","注册成功!");
        }else{
            return oftenCheck.isUserAccount(null,"注册失败!");
        }
    }


    /**
     * 将常用的语句单独写成一个方法，减少代码冗余
     * **/
    public Tool isUserAccount(Object users,String msg)
    {
        Tool result = null;
        if(users ==null)
        {
            result = new Tool(false,
                    404,
                    msg,
                    null);
            return result;
        }else
        {
            result = new Tool(true,
                    200,
                    msg,
                    users);
            return result;
        }
    }

    public void Tres(Tool result)
    {
        try {
            JsonUtil.toJson(result, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    /**这个方法需要重写，代码复用性太差**/
    public  List<UserAccount> getListUser(List<UserAccount> userAccount)
    {
        List<UserAccount> listuserAccount = new ArrayList<>();
        for (UserAccount entity : userAccount) {
            UserAccount usera = new UserAccount();
            usera.setDate(entity.getDate());
            usera.setName(entity.getName());
            usera.setEmail(entity.getEmail());
            usera.setId(entity.getId());
            usera.setStatuscode(entity.getStatuscode());
            usera.setTime(entity.getTime());
            usera.setAge(entity.getAge());
            usera.setSex(entity.getSex());
            listuserAccount.add(usera);
        }
        return listuserAccount;
    }






}
